<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="par-nav-bar" title="登录">
      <van-icon slot="right" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 导航栏 -->
    <div class="PositionCentered">
      <!-- 登录表单 -->
      <van-form @submit="onSubmit" ref="loginForm">
        <van-field
          name="mobile"
          maxlength="11"
          placeholder="请输入手机号"
          v-model="user.mobile"
          :rules="userFormRules.mobile"
          type="number"
        >
          <i slot="left-icon" class="toutiao toutiao-shouji"></i>
        </van-field>
        <van-field
          type="password"
          maxlength="6"
          v-model="user.code"
          name="code"
          placeholder="请输入验证码"
          :rules="userFormRules.code"
        >
          <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
          <template #button>
            <van-count-down
              v-if="isCountDownShow"
              slot="button"
              :time="1000 * 60"
              format="ss 秒"
              @finish="isCountDownShow = false"
            />
            <van-button
              v-else
              round
              slot="button"
              size="small"
              type="primary"
              @click="onSendSms"
              class="backGround"
              native-type="button"
              >发送验证码</van-button
            >
          </template>
        </van-field>
        <div class="login-btn-wrap">
          <van-button class="login-btn" block type="info" native-type="submit">
            登录
          </van-button>
        </div>
      </van-form>
      <!-- /登录表单 -->
    </div>
  </div>
</template>
<script>
import { login, sendSms } from '@/api/user.js'
export default {
  name: 'LoginIndex',
  components: {},
  props: {},
  data () {
    return {
      isCountDownShow: '',
      user: {
        mobile: '15592426925',
        code: '246810'
      },
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: '手机格式错误'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空！'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    // 登录
    async onSubmit () {
      // onSubmit() {
      // 获取数据
      // |验证
      this.$toast.loading({
        duration: 0, // 持续时间，0表示持续展示不停止
        forbidClick: true, // 是否禁止背景点击
        message: '登录中...' // 提示消息
      })
      // |提交请求登录 |根据返回数据进行操作
      try {
        const { data } = await login(this.user)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功')
        this.$router.back()
      } catch (err) {
        // try catch???因为后台登录失败的时候状态码是400，会报错，所以需要进行try catch
        // 当前企业中真实写法无需try catch,如果前后台接口一旦可以调通，那么后台接口的状态码必然是200
        if (err.response.status === 400) {
          this.$toast.fail('验证码错误！')
        } else {
          this.$toast.fail('登录失败！')
        }
      }
    },
    // 发送验证码
    async onSendSms () {
      // 1.校验 2.通过倒计时 3.发送验证码
      console.log('onSendSms')
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.log('验证失败', err)
      }

      this.$toast.loading({
        duration: 0, // 持续时间，0表示持续展示不停止
        forbidClick: true, // 是否禁止背景点击
        message: '发送中...' // 提示消息
      })
      try {
        await sendSms(this.user.mobile)
        this.isCountDownShow = true
        this.$toast('发送成功了！')
      } catch (err) {
        this.isCountDownShow = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，稍后重试')
        } else if (err.response.status === 404) {
          this.$toast('手机号不正确！')
        } else {
          this.$toast('发送失败，稍后重试')
        }
      }
    }
  }
}
/* 老师的方法 不全
onSendSms(){
  // 检验手机号码是否合法
  this.$refs.loginForm
  .validate('mobile')
  .then(res=>{
    console.log('成功')
    // 倒计时
    this.isCountDownShow=true;
    //发送验证码
    this.sendSmsFn()
    }).catch((err)=>{
      console.log(err.message);
    })
    },
    //发送手机验证码！
    sendSmsFn(){
      sendSms(this.user.mobile).then(res=>{
        console.log("发送成功！");
      }).catch(err=>{
        console.log("发送失败");
        console.log(err)
      })
    }
*/
</script>
/*
你要想，让中间的盒子水平居中的话定义也必须要和上面的标题，相对应，也就是说父盒子是标题绝对定位，子盒子是中间的盒子绝对定位
 */
<style scoped lang="less">
.login-container {
  background: transparent !important;
  .toutiao {
    font-size: 37px;
  }
  .van-nav-bar {
    position: relative;
    background-image: linear-gradient(45deg, #fa8bff, #2bd2ff 52%, #2bff88 90%);
    background-repeat: no-repeat;
    /deep/.van-nav-bar__title {
      color: #ef6eae !important;
    }
  }
  .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }

  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
    background-image: linear-gradient(0deg,#d9afd9,#97d9e1);
    border-radius: 0.2rem;
    border: none;
    box-shadow: 0 0 0.2rem #d9afd9;
    font-family: 宋体;
    font-size: .53333rem;
    font-weight: 600;
    }
  }
  .PositionCentered {
    width: 580px;
    height: 410px;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
    padding: 10px;
  }
  .toutiao{
    color: #ef6eae;
  }
}

</style>
